<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script th:src="@{/}+'js/tool.js'"></script>
  <script type="text/javascript" src="js/ueditor-plus/ueditor.config.js"></script>
  <script type="text/javascript" src="js/ueditor-plus/ueditor.all.js"></script>
  <script type="text/javascript" charset="utf-8" src="js/ueditor-plus/lang/zh-cn/zh-cn.js?58c38108"></script>
  <style type="text/css">
	.layui-form .layui-form-item{
		width:90%;
	}
	.layui-input-inline{
		width:350px!important
	}
	</style>

</head>
<body style="margin-top: 100px">
<form class="layui-form" lay-filter="myform" id="myform">
<input type="hidden" name="delFlag" id="delFlag" value="1">
<input type="hidden" name="recordId" id="recordId">
<input type="hidden" name="id" id="id" th:value="${id}">
<input type="hidden" name="libQuestionId" th:value="${libQuestionId}">
  <div class="layui-form-item">
    <label class="layui-form-label">选项文本</label>
    <div class="layui-input-block">
      <script id="xxwb" type="text/plain" style="height:130px;"></script>
<!--      <textarea id="xxwb" name="xxwb"></textarea>-->
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">索引</label>
    <div class="layui-input-block">
      <input type="radio" name="indexLetter" value="A" title="A">
      <input type="radio" name="indexLetter" value="B" title="B">
      <input type="radio" name="indexLetter" value="C" title="C">
      <input type="radio" name="indexLetter" value="D" title="D">
      <input type="radio" name="indexLetter" value="E" title="E">
      <input type="radio" name="indexLetter" value="F" title="F">
      <input type="radio" name="indexLetter" value="G" title="G">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">正确1答案</label>
    <div class="layui-input-inline">
      <input type="checkbox" name="right" lay-skin="switch">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">答案说明</label>
    <div class="layui-input-block">
      <script id="dasm" type="text/plain" style="height:130px;"></script>
<!--      <textarea id="dasm" name="dasm"></textarea>-->
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">登记时间</label>
    <div class="layui-input-inline">
      <input type="text" name="createTime" id="createTime" th:value="${currentTime}" readonly="readonly" class="layui-input">
    </div>
  </div>
  <button id="submitBtn" class="layui-btn" style="display:none" lay-submit lay-filter="submitme">保存</button>
</form>
<script src="js/editor/ckeditor.js"></script>
<script src="js/editor/config.js"></script>
<script>
  var id = $("#id").val();
  (function () {
    var xxwb = UE.getEditor('xxwb', {
    });
    var dasm = UE.getEditor('dasm', {
    });
    var entity = getEntityInfo(id,"libChoice/getById");
    xxwb.ready(function(){
      try{
        UE.getEditor('xxwb').setContent(entity.text, false)
      }catch (e){

      }
    });
    dasm.ready(function(){
      try{
        UE.getEditor('dasm').setContent(entity.text, false)
      }catch (e){

      }
    });

  })();
// CKEDITOR.replace('xxwb',
//         {
//           filebrowserImageUploadUrl: "uploadImage/upload?winname=choicewin",
//           height: 150
//         });
// CKEDITOR.replace('dasm',
//         {
//           filebrowserImageUploadUrl: "uploadImage/upload?winname=choicewin",
//           height: 150
//         });
layui.use(['form', 'table', 'laydate'], function(){
	  var form = layui.form
	  ,layer = layui.layer;
	  form.on('submit(submitme)', function(data){
          // var text = CKEDITOR.instances.xxwb.getData();
          // text = substractText(text);
        var text = UE.getEditor('xxwb').getContent();
          if(text.length<1){
            top.layer.msg("选项文本字数不能小于1");
            return false
          }
          // var dasm = CKEDITOR.instances.dasm.getData();
          var dasm = UE.getEditor('dasm').getContent();
          if($(dasm).text()!=''){
            dasm = $(dasm).text();
          }
          var values = {text:text,answerText:dasm};
          $(":input").each(function(i,v){
				if(v.name)
				  values[v.name] = v.value;

			})
            if(!values.id){
              delete values.id;
            }
			values["right"] = $(":checkbox[name=right]").is(":checked")?1:2;
			let ic = $(":radio[name=indexLetter]:checked");
			if(ic.length===0){
				layer.msg("请选择索引");
				return false;
			}
			values["indexLetter"] = ic.val();
			axios.post("libChoice/save",values).then(res=>{
				if(res.data.code==1){
					layer.msg(ret.data.msg);
				}else{
                    var win = getTabWin();
                    try{
                      win.loadGrid('main-table');
                    }catch (e){
                      win = getTabWin('editwin');
                      win.loadGrid('main-table');
                    }

					layer.msg("保存成功");
					var index = top.layer.getFrameIndex(window.name);
				    top.layer.close(index);
				}
			})
			return false; 
		  });
	  if(id){
		  loadData()
	  }
	  function loadData(){
		var entity = getEntityInfo(id,"libChoice/getById");
		form.val("myform",entity);
        // CKEDITOR.instances.xxwb.setData(entity.text);
        // CKEDITOR.instances.dasm.setData(entity.answerText);
		if(entity.right!==1){
			$(":checkbox[name=right]").prop("checked",false);
			form.render();
		}
       
	  }
	  
})

</script>
</body>
</html>